/*******************************************************************************
 * Copyright (c) Jonathan Buck - jonbuck16@gmail.com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    jonbuck16@gmail.com - initial API and implementation
 *******************************************************************************/
package net.jonbuck.tassoo.ui.wizard.page;

import net.jonbuck.tassoo.persistence.dao.TassooDao;
import net.jonbuck.tassoo.persistence.dao.TassooDaoImpl;
import net.jonbuck.tassoo.ui.wizard.model.NewContainerModel;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.wizard.WizardPageSupport;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.core.runtime.IStatus;

/**
 * 
 * 
 * @since 1.0.0
 */
public class ContainerDetailsPage extends WizardPage {
	private DataBindingContext m_bindingContext;
	private TassooDao tassooDao = TassooDaoImpl.getInstance();
	public static String PAGE_NAME = "net.jonbuck.tassoo.ui.wizard.page.ContainerDetailsPage"; //$NON-NLS-1$
	private NewContainerModel newContainerModel;
	private Text txtName;
	private Text txtDescription;

	/**
	 * 
	 * @param pageName
	 * @param newContainerModel
	 */
	public ContainerDetailsPage(String pageName, NewContainerModel newContainerModel) {
		super(pageName);
		setDescription("Enter the details for the new container");
		setTitle("New Container");
		this.newContainerModel = newContainerModel;
	}

	/**
	 * 
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		GridLayout gl_container = new GridLayout();
		gl_container.numColumns = 2;
		container.setLayout(gl_container);
		setControl(container);

		Label lblName = new Label(container, SWT.NONE);
		lblName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblName.setText("Name:");

		txtName = new Text(container, SWT.BORDER);
		txtName.setText("");
		txtName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Label lblDescription = new Label(container, SWT.NONE);
		lblDescription.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		lblDescription.setText("Description:");

		txtDescription = new Text(container, SWT.BORDER | SWT.WRAP | SWT.MULTI);
		GridData gd_txtDescription = new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1);
		gd_txtDescription.heightHint = 50;
		txtDescription.setLayoutData(gd_txtDescription);

		m_bindingContext = initDataBindings();
		initBindingSupport();
	}

	/**
	 * 
	 */
	private void initBindingSupport() {
		WizardPageSupport.create(this, m_bindingContext);
	}

	/**
	 * 
	 * @return
	 */
	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		IObservableValue txtNameObserveTextObserveWidget = SWTObservables.observeText(txtName, SWT.Modify);
		IObservableValue newContainerModelContainerNameObserveValue = PojoObservables.observeValue(newContainerModel,
				"containerName");
		UpdateValueStrategy nameStrategy = new UpdateValueStrategy();
		nameStrategy.setBeforeSetValidator(new IValidator() {
			public IStatus validate(Object value) {
				if (!tassooDao.isLoaded()) {
					return ValidationStatus.error("A Task Store has not been loaded");
				} else {
					String containerName = (String) value;
					if (StringUtils.isBlank(containerName)) {
						return ValidationStatus.error("A name for the container is required.");
					}
				}
				return ValidationStatus.ok();
			}

		});
		bindingContext.bindValue(txtNameObserveTextObserveWidget, newContainerModelContainerNameObserveValue, nameStrategy, null);
		//
		IObservableValue txtDescriptionObserveTextObserveWidget = SWTObservables.observeText(txtDescription, SWT.Modify);
		IObservableValue newContainerModelContainerDescriptionObserveValue = PojoObservables.observeValue(newContainerModel,
				"containerDescription");
		UpdateValueStrategy descriptionStrategy = new UpdateValueStrategy();
		descriptionStrategy.setBeforeSetValidator(new IValidator() {
			public IStatus validate(Object value) {
				if (!tassooDao.isLoaded()) {
					return ValidationStatus.error("A Task Store has not been loaded");
				} else {
					String containerDescription = (String) value;
					if (StringUtils.isBlank(containerDescription)) {
						return ValidationStatus.error("A description for the container is required.");
					}
				}
				return ValidationStatus.ok();
			}

		});
		bindingContext.bindValue(txtDescriptionObserveTextObserveWidget, newContainerModelContainerDescriptionObserveValue,
				descriptionStrategy, null);
		//
		return bindingContext;
	}
}
